﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Snoop.Web.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:FieldSet runat=server></{0}:FieldSet>")]
    [ParseChildren(true, "Fields"),]
    public class FieldSet : System.Web.UI.WebControls.WebControl, INamingContainer
    {
        public FieldSet()
        {
            //return ((list == null) ? new LabeledTextBox[5] : list);
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Legend
        {
            get
            {
                String s = (String)ViewState["Legend"];
                return ((s == null) ? string.Empty : s);
            }

            set
            {
                ViewState["Legend"] = value;
            }
        }

        List<LabeledTextBox> list;


         [Category("Behavior"),
        Description("Fields collection"),
        DesignerSerializationVisibility(
            DesignerSerializationVisibility.Content),
        
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public List<LabeledTextBox>  Fields
        {
            get
            {
                if (list == null)
                    list = new List<LabeledTextBox>();
                return list;
            }
        }

        protected override void CreateChildControls()
        {
            if(list != null)
            base.CreateChildControls();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.WriteBeginTag("fieldset");
            writer.WriteAttribute("ID", this.ID);
            writer.WriteAttribute("runat", "server");
            writer.Write(">");
            writer.WriteFullBeginTag("legend");
            writer.Write(Legend);
            writer.WriteEndTag("legend");

            if (list != null)
            {

                writer.WriteFullBeginTag("ol");

                foreach (var txt in list)
                {
                    writer.WriteFullBeginTag("li");

                    txt.RenderControl(writer);

                    writer.WriteEndTag("li");
                }

                writer.WriteEndTag("ol");
            }
            writer.WriteEndTag("fieldset");
            //base.Render(writer);
        }

    }
}
